package com.chenwenxuan.sync.ui.tree;

import java.awt.Component;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import com.chenwenxuan.sync.console.model.Task;
import com.chenwenxuan.sync.console.model.TaskCatalog;
import com.chenwenxuan.sync.ui.ImageIconResources;

public class TaskTreeRender extends DefaultTreeCellRenderer {

	private static final long serialVersionUID = -3984547664508386341L;

	public TaskTreeRender() {
		super();
	}

	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
			boolean leaf, int row, boolean hasFocus) {

		super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
		
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
		
		Object userObject = node.getUserObject();
		
		if ( userObject instanceof TaskCatalog ) {
			setText(((TaskCatalog) userObject).getTaskCatalogName());
			if ( expanded ) {
				setIcon(ImageIconResources.getIconImage(ImageIconResources.ICON_TREE_ICON_FOLDER_OPEN));
			} else {
				setIcon(ImageIconResources.getIconImage(ImageIconResources.ICON_TREE_ICON_FOLDER));
			}
		} else if ( userObject instanceof Task ) {
			setText(((Task) userObject).getTaskName());
		} 
		
		return this;

	}

}
